/**
 * @className: AttendanceGroupService
 * @author: Csh
 * @date: 2024/3/19 21:34
 * @Version: 1.0
 * @description:
 */
package com.zhuojy.equity.mall.admin.service.AttendanceServices;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.AttendanceGroup;
import com.zhuojy.equity.mall.admin.common.entity.ClockSet;
import com.zhuojy.equity.mall.admin.common.entity.TUser;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.AttendanceGroupMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.ClockSetMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.DemoMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class AttendanceGroupService extends ServiceImpl<AttendanceGroupMapper, AttendanceGroup> {
    @Autowired
    private DemoMapper demoMapper;

    @Autowired
    private ClockSetMapper clockSetMapper;
    public ResultEntity findAll(AttendanceGroup attendanceGroup, Integer pageNum, Integer pageSize) {
        Page page = new Page<>(pageNum, pageSize);
        IPage<AttendanceGroup> myPage = this.baseMapper.findAll(page, attendanceGroup);

        for (AttendanceGroup record : myPage.getRecords()) {
            record.setNum(demoMapper.selectCount(Wrappers.lambdaQuery(TUser.class).eq(TUser::getAttendanceGroupId,record.getId())));
        }
        Map map = new HashMap<>();
        map.put("total",myPage.getTotal());
        map.put("rows",myPage.getRecords());

        return ResultEntity.OK(map);
    }

    public int addOrUpAttendance(AttendanceGroup attendanceGroup) {

        int insert = 0;
        if(attendanceGroup.getId()==null){
            ClockSet clockSet = attendanceGroup.getClockSet();
            clockSetMapper.insert(clockSet);
            attendanceGroup.setCreatedAt(new Date());
            attendanceGroup.setAttendanceTypeId(clockSet.getId());
            insert = this.baseMapper.insert(attendanceGroup);
        }else{
            clockSetMapper.updateById(attendanceGroup.getClockSet());
            attendanceGroup.setUpdateAt(new Date());
            insert = this.baseMapper.updateById(attendanceGroup);
        }

        return insert;
    }

    public AttendanceGroup findOneAttendance(Integer id) {
        AttendanceGroup attendanceGroup = this.baseMapper.selectById(id);
        attendanceGroup.setClockSet(clockSetMapper.selectById(attendanceGroup.getAttendanceTypeId()));
        return attendanceGroup;
    }

    public Integer delAttendance(AttendanceGroup attendanceGroup) {
        System.out.println("ddddddddddddddddddddddddddddddddd");
        clockSetMapper.deleteById(attendanceGroup.getClockSets().getId());
        return this.baseMapper.deleteById(attendanceGroup.getId());
    }
}
